define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pro/activity/index',
                    add_url: 'pro/activity/add',
                    edit_url: 'pro/activity/edit',
                    del_url: 'pro/activity/del',
                    multi_url: 'pro/activity/multi',
                    table: 'gzns_course',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'create_time',
                sortOrder: "desc",
                queryParams: function (params) {
                    if (!params.filter) {
                        params.filter={}
                    }else {
                        params.filter = JSON.parse(params.filter)
                    }
                    if (!params.op) {
                        params.op={}
                    }else {
                        params.op = JSON.parse(params.op)

                    }
                    if (!params.filter.type && !params.op.type) {
                        params.filter.type = '3';
                        params.op.type = '='
                    }

                    params.filter = JSON.stringify(params.filter)
                    params.op = JSON.stringify(params.op)

                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'category.name', title: __('Category.name'),operate: 'Like'},
                        // {field: 'category_id', title: __('Category_id')},
                        {field: 'name', title: __('Name'),operate: 'Like'},
                        {field: 'cover', title: __('Cover'), formatter: Table.api.formatter.image,operate: false},
                        // {field: 'summary', title: __('Summary')},
                        // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        // {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.status},
                        // {field: 'column', title: __('Column')},
                        // {field: 'address', title: __('Address')},
                        // {field: 'short_address', title: __('Short_address')},
                        {field: 'time', title: __('Time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'lat', title: __('Lat')},
                        // {field: 'lng', title: __('Lng')},
                        // {field: 'mail', title: __('Mail')},
                        // {field: 'phone', title: __('Phone')},
                        // {field: 'teacher_id', title: __('Teacher_id')},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'people_limit', title: __('People_limit')},
                        // {field: 'comment_count', title: __('Comment_count')},
                        // {field: 'buy_count', title: __('Buy_count')},
                        // {field: 'wx_card_id', title: __('Wx_card_id')},
                        // {field: 'is_ticket', title: __('Is_ticket')},
                        // {field: 'ticket_title', title: __('Ticket_title')},
                        {field: 'is_recommend', title: __('Is_recommend'), searchList: {"0":__('Is_recommend 0'),"1":__('Is_recommend 1')}, formatter: Table.api.formatter.toggle},
                      {field: 'check_status', title: __('Check_status'), searchList: {"1":__('Check_status 1'), "3":__('Check_status 3'), "4":__('Check_status 4'), "2":__('Check_status 2')}, formatter: Table.api.formatter.status},

                      // {field: 'is_home', title: __('Is_home'), searchList: {"0":__('Is_home 0'),"1":__('Is_home 1')}, formatter: Table.api.formatter.toggle},
                        // {field: 'is_notice', title: __('Is_notice')},
                        // {field: 'wjx_status', title: __('Wjx_status'), searchList: {"0":__('Wjx_status 0'),"1":__('Wjx_status 1')}, formatter: Table.api.formatter.toggle},
                        // {field: 'wjx_id', title: __('Wjx_id')},
                        // {field: 'history_content', title: __('History_content')},
                        // {field: 'teacher.name', title: __('Teacher.name'),operate: 'Like'},
                        // {field: 'config.title', title: __('Config.title'),operate: 'Like'},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                      // {field: 'sort', title: __('Sort')},
                      {field: 'operate', title: __('Operate'),
                        table: table,
                        events: Table.api.events.operate,
                        formatter: Table.api.formatter.operate,
                        buttons: [
                          {
                            name: 'submit_check',
                            text: __('提交审核'),
                            title: __('提交审核'),
                            classname: 'btn btn-xs btn-success btn-ajax',
                            confirm: '确定提交审核吗？',
                            url: 'pro/activity/submitCheck',
                            visible: function (row) {
                              return row.check_status == 1;
                            },
                            success: function () {
                              table.bootstrapTable('refresh');
                            }
                          },
                          {
                            name: 'check_result',
                            text: __('审核结果'),
                            title: __('审核结果'),
                            classname: 'btn btn-xs btn-success btn-dialog',
                            url: 'pro/activity/showCheckResult',
                            visible: function (row) {
                              return row.check_status == 3 || row.check_status == 4;
                            }
                          }
                        ]
                      }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit,.btn-add").data("area", ["100%", "100%"]);
            });
        },
        add: function () {

            Controller.api.bindevent();
            $(document).on("keydown", "#suggestId", function(e){
                if(e.keyCode == 13 || e.keyCode == 10){
                    return false;
                }
            });
            var marker = null;

            function G(id) {
                return document.getElementById(id);
            }
            var map = new qq.maps.Map(document.getElementById("l-map"),{
                center:  new qq.maps.LatLng(39.916527,116.397128),
                zoom: 13
            });
            //实例化自动完成
            var ap = new qq.maps.place.Autocomplete(document.getElementById('suggestId'), {
                offset: new qq.maps.Size(0, 5),
                location: '北京市'
            });
            //调用Poi检索类。用于进行本地检索、周边检索等服务。
            var searchService = new qq.maps.SearchService({
                complete : function(results){
                    if(results.type === "CITY_LIST") {
                        alert("当前检索结果分布较广，请指定城市进行检索");
                        return;
                    }
                    if(marker) marker.setMap(null)
                    var pois = results.detail.pois;
                    var latlngBounds = new qq.maps.LatLngBounds();
                    var poi = pois[0];
                    G('c-lat').value = poi.latLng.lat;
                    G('c-lng').value = poi.latLng.lng;
                    latlngBounds.extend(poi.latLng);
                    marker = new qq.maps.Marker({
                        map:map,
                        position: poi.latLng
                    });
                    marker.setTitle(poi.name);
                    map.fitBounds(latlngBounds);
                }
            });
            //添加监听事件
            qq.maps.event.addListener(ap, "confirm", function(res){
                searchService.search(res.value);
            });
        },
        edit: function () {
            Controller.api.bindevent();
            $(document).on("keydown", "#suggestId", function(e){
                if(e.keyCode == 13 || e.keyCode == 10){
                    return false;
                }
            });
            var marker = null;

            function G(id) {
                return document.getElementById(id);
            }
            var map = new qq.maps.Map(document.getElementById("l-map"),{
                center:  new qq.maps.LatLng(39.916527,116.397128),
                zoom: 13
            });
            //实例化自动完成
            var ap = new qq.maps.place.Autocomplete(document.getElementById('suggestId'), {
                offset: new qq.maps.Size(0, 5),
                location: '北京市'
            });
            //调用Poi检索类。用于进行本地检索、周边检索等服务。
            var searchService = new qq.maps.SearchService({
                complete : function(results){
                    if(results.type === "CITY_LIST") {
                        alert("当前检索结果分布较广，请指定城市进行检索");
                        return;
                    }
                    if(marker) marker.setMap(null)
                    var pois = results.detail.pois;
                    var latlngBounds = new qq.maps.LatLngBounds();
                    var poi = pois[0];
                    G('c-lat').value = poi.latLng.lat;
                    G('c-lng').value = poi.latLng.lng;
                    latlngBounds.extend(poi.latLng);
                    marker = new qq.maps.Marker({
                        map:map,
                        position: poi.latLng
                    });
                    marker.setTitle(poi.name);
                    map.fitBounds(latlngBounds);
                }
            });
            //添加监听事件
            qq.maps.event.addListener(ap, "confirm", function(res){
                searchService.search(res.value);
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
